#include "RLFEngine.h"
#include <stdio.h>

RLFEngine *rlf = NULL;

// Use this last.
__declspec(dllexport) void destroy()
{
	if(rlf != NULL)
	{
		delete(rlf);
		rlf = NULL;
	}
}

// Use this first.
__declspec(dllexport) void initialize(int* inputPixels, int pixelsLength, int imageWidth, int sampleSizeX, int sampleSizeY)
{
	destroy();
	rlf = new RLFEngine(inputPixels, pixelsLength, imageWidth, sampleSizeX, sampleSizeY);
}

// Use this second.
__declspec(dllexport) void calculateForDirection(int direction, int windowSizeX, int windowSizeY)
{
	PrimitiveDirection dir;
	switch(direction)
	{
	case 0:
		dir = PrimitiveDirection::Horizontal;
		break;
	case 1:
		dir = PrimitiveDirection::Vertical;
		break;
	case 2:
		dir = PrimitiveDirection::Both;
		break;
	default:
		throw new exception("Direction should be 0, 1 or 2.");
	}

	rlf->calculateStatistics(dir, windowSizeX, windowSizeY);
}

/* There are 15 images. First 5 is for horizontal, next 5 for vertical, last one for primitives in both directions.
	In one chain of 5 images the order is: SPE, LPE, GLU, PLU, PP.
	Use this third.*/
__declspec(dllexport) void getRLFImage(int *rlfImage ,int imageNumber)
{
	PrimitiveDirection dir;
	RunLengthStatistic stat;

	int dirIndex = imageNumber/5;
	switch(dirIndex)
	{
	case 0:
		dir = PrimitiveDirection::Horizontal;
		break;
	case 1:
		dir = PrimitiveDirection::Vertical;
		break;
	case 2:
		dir = PrimitiveDirection::Both;
		break;
	default:
		throw new exception("imageNumber should be in range <0,14>.");
	}

	int statIndex = imageNumber%5;
	switch(statIndex)
	{
	case 0:
		stat = RunLengthStatistic::ShortPrimitiveEmphasis;
		break;
	case 1:
		stat = RunLengthStatistic::LongPrimitiveEmphasis;
		break;
	case 2:
		stat = RunLengthStatistic::GrayLevelUniformity;
		break;
	case 3:
		stat = RunLengthStatistic::PrimitiveLengthUniformity;
		break;
	case 4:
		stat = RunLengthStatistic::PrimitivePercentage;
		break;
	default:
		throw new exception("imageNumber should be in range <0,14>.");
	}

	int *srcImage = rlf->getRLFImage(dir, stat);
	// copying the image for the caller of the function
	memcpy((void*) rlfImage, (void*)srcImage, rlf->getOutputPixelCount() * sizeof(int));
}